package com.webshooter.math;

public class MathUtil {

    public static int clamp(int value, int min, int max) {
        return Math.max(min, Math.min(max, value));
    }

    public static float clamp(float value, float min, float max) {
        return Math.max(min, Math.min(max, value));
    }

    public static float pointToLineSegmentDistance(Vector a, Vector b, Vector c) {
        float l2 = b.subtract(a).normSquared();
        if (l2 == 0.0) {
            return c.subtract(a).norm();
        }
        float t = c.subtract(a).dotProduct(b.subtract(a)) / l2;
        if (t < 0.0) {
            return c.subtract(a).norm();
        } else if (t > 1.0) {
            return c.subtract(b).norm();
        }
        Vector projection = a.add(b.subtract(a).multiply(t));
        return c.subtract(projection).norm();
    }
}
